/**
 * Created by 55456 on 2016/4/11.
 */
var db = require("../../models/index");
var codeConfig = require("../../configs/codeConfig");
var appConfig  = require("../../configs/appConfig");
var auth = require("../../helpers/auth");
var autoPrefix = appConfig.autoPrefix;
var commentAdaptor = require("../../modelsAdaptor/commentAdaptor");

module.exports = (router) => {
    router.post(autoPrefix("comment/:storyID"),function *(){
        this.checkParams("storyID").notEmpty();
        this.checkBody("commentContent").notEmpty();
        if(this.errors){
            this.body = codeConfig.parseErrorMsg(this.errors);
            return;
        }
        var body = this.request.body;
        var userID = (yield auth.user(this)).id;
        var storyID = this.params.storyID;
        var msg = yield commentAdaptor.addComment(userID,storyID,body.commentContent);
        this.body = msg.status ? codeConfig.parseMsg({ id : msg.id}) : codeConfig.parseErrorMsg(msg.err);
    });

    router.post(autoPrefix("reply/:commentID"),function *(){
        this.checkParams("commentID").notEmpty();
        this.checkBody("replyContent").notEmpty();
        if(this.errors){
            this.body = codeConfig.parseErrorMsg(this.errors);
            return;
        }
        var body = this.request.body;
        var userID = (yield auth.user(this)).id;
        var commentID = this.params.commentID;
        var msg = yield commentAdaptor.addReply(userID,commentID,body.replyContent);
        this.body = msg.status ? codeConfig.parseMsg({ id : msg.id}) : codeConfig.parseErrorMsg(msg.err);
    });

    router.post(autoPrefix("commentlist/:storyID"),function *(){
        this.checkParams("storyID").notEmpty();
        if(this.errors){
            this.body = codeConfig.parseErrorMsg(this.errors);
            return;
        }
        var storyID = this.params.storyID;
        var msg = yield commentAdaptor.getAllComment(storyID);
        this.body = msg.status ? codeConfig.parseMsg({ comment:msg.comment }) : codeConfig.parseErrorMsg(msg.err);
    });
};